<template>
  <div class="student-profile">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <h3>个人信息</h3>
        </div>
      </template>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户名">
          {{ userStore.username }}
        </el-descriptions-item>
        <el-descriptions-item label="角色">
          <el-tag type="success">学生</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="加入的老师">
          <template v-if="teacherInfo">
            <div class="teacher-info">
              <el-tag type="info" class="teacher-tag">{{ teacherInfo.username }}</el-tag>
              <span class="teacher-name">{{ teacherInfo.realName }}</span>
            </div>
          </template>
          <template v-else>
            <el-tag type="warning">暂未加入任何老师</el-tag>
            <el-button 
              type="primary" 
              link 
              @click="$router.push('/student/join-teacher')"
              style="margin-left: 10px"
            >
              去加入
            </el-button>
          </template>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'StudentProfilePage'
}
</script>

<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/store/user'
import { getMyTeacher } from '@/api/student'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const teacherInfo = ref(null)

const fetchTeacherInfo = async () => {
  try {
    const response = await getMyTeacher()
    console.log('Teacher Info Response:', response)
    if (response.code === 200 && response.data) {
      teacherInfo.value = response.data
    }
  } catch (error) {
    console.error('获取老师信息失败:', error)
    // 如果是400错误，说明未加入老师，这是正常的
    if (error.response?.status !== 400) {
      ElMessage.error(error.response?.data?.message || '获取老师信息失败')
    }
  }
}

onMounted(() => {
  fetchTeacherInfo()
})
</script>

<style scoped>
.student-profile {
  max-width: 800px;
  margin: 0 auto;
}

.profile-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.teacher-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.teacher-tag {
  margin-right: 10px;
}

.teacher-name {
  color: #606266;
}

:deep(.el-descriptions-item__content) {
  display: flex;
  align-items: center;
}
</style> 